/* 
 * File:   serveur.c
 * Author: champion
 *
 * Created on 27 octobre 2009, 17:50
 */

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h> 
#include <sys/stat.h>
#include <string.h>
#include <rpc/rpc.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <pwd.h>
#include "myFtp.h"
#include "tools.h"


/*
 *  pwd_1_svc  renvoi le repertoire courant
 */
char ** pwd_1_svc(void *none, struct svc_req *rqstp){

    static char* result;
    setPwd(rqstp);
    char path[MAXPATHLEN];
    getcwd(path, MAXPATHLEN);
    result=path;
    return(&result);
};

/*
 *  cd_1_svc  change le repertoire de travail
 */
bool_t * cd_1_svc(char **args, struct svc_req *rqstp){

    static bool_t result;
    char cmd[256];
    int out;
    setPwd(rqstp);
    result=1;

    sprintf(cmd, "%s", *args);
    if(strcmp(*args,"null")==0)
       sprintf(cmd, ".");

    out=chdir(cmd);
    updatePwd(rqstp);
    if(out!=0)
      result=0;
    return(&result);
};

/*
 *  ls_1_svc  liste le contenu d'un repertoire
 */

char ** ls_1_svc(void *none, struct svc_req *rqstp){

    static char** result;
    char cmd[256];
    setPwd(rqstp);
    sprintf(cmd, ".");
    result=getdir(cmd);
    return(result);
};

/*
 *  get_1_svc  téléchargement d'un fichier
 */
char ** get_1_svc(char **fichier, struct svc_req *rqstp){

    static char * result="ok";
    setPwd(rqstp);
    return(&result);
};

/*
 *  put_1_svc  upload d'un fichier
 */
int * put_1_svc(char **fichier, struct svc_req *rqstp){

    static int result;
    setPwd(rqstp);

    result=1;
    return(&result);
};


/*
 *  open_1_svc  ouverture d'un fichier 
 */
int * open_1_svc(open_file *args, struct svc_req *rqstp){

    static int result;
    long size;
    FILE *fic;
    char *rw="w";
    if(!args->rw)
        rw="r";
    fic = fopen(args->filename, rw);

    fseek (fic , 0 , SEEK_END);
    size = ftell (fic);
    fclose(fic);

    //fixe le descripteur de fichier
    result= open(args->filename,O_RDWR);
    //donne la taille du fichier
    //result.size = size;
    return(&result);
};

/*
 *  read_1_svc  lecture dans un fichier 
 */

data_i * read_1_svc(int *fd, struct svc_req *rqstp){

    static data_i result;
    int k = 0,r=0;
    long out,size;
    FILE *fic;
    
    fic=fdopen(*fd,"r");
    size=ftell(fic);
    result.buf = (char*) malloc (sizeof(char)*BUFF_SIZE);

    if(size == 0){
    	while ((read(*fd,&(Buffer_Reader[k]),1) != -1) && k < MAX_BUFF_SIZE){
		if( k< BUFF_SIZE){
		      result.buf[k] = Buffer_Reader[k];
                      r++;
		}
		k++;
    	}
    result.buf[r-1] = '\0';
    }
    else{
	int s =0;
	int t = 0;
	t = size;
	for( t= size, s=0;Buffer_Reader[t]!='\0' && s < BUFF_SIZE ;s++,t++){
		result.buf[s] = Buffer_Reader[t];
	}

    	result.buf[t-1] = '\0';
	fputs(result.buf,stderr);
    }
    result.len = strlen(result.buf);
    out=ftell(fic)+result.len;
    
    fseek(fic,out,SEEK_SET);
    return(&result);
};

/*
 *  write_1_svc  ecriture dans un fichier
 */
int * write_1_svc(data_o *data, struct svc_req *rqstp){

    static int result;
    lseek(data->fd,0,SEEK_END);
    write(data->fd,data->buf,data->len);
    result=1;
    return(&result);
};

/*
 *  close_1_svc  fermeture d'un fichier
 */
int * close_1_svc(int *fd, struct svc_req *rqstp){

    static int result;
    result = close(*fd);
    return(&result);
};

/*
 *  connect_1_svc  ajouter un utilisateur
 */
authparms * connect_1_svc(char **username, struct svc_req *rqstp)
{
    static struct authparms result;
    passwdParams=NULL;
    passwdParams=getpwnam(*username);
    if(passwdParams==NULL)
    {
        result.uid=-1;
        result.gid=-1;
    }else{

    result.username =passwdParams->pw_name;
    result.uid =(int)passwdParams->pw_uid;
    result.gid=(int)passwdParams->pw_gid;

    }
    return(&result);

};

/*
 *  bye_1_svc  sortie du programme
 */
int * bye_1_svc(void *none, struct svc_req *rqstp){

    static int result;
    char *ip;
    int port;
    ip = inet_ntoa(rqstp->rq_xprt->xp_raddr.sin_addr);
    port = (int)rqstp->rq_xprt->xp_raddr.sin_port;
    liste_Clients = supprimer(liste_Clients, ip,port);
    result=1;
    return(&result);
};

